/*
 * Created by SharpDevelop.
 * User: c0148643
 * Date: 11/05/2010
 * Time: 11:12
 * 
 * To change this template use Tools | Options | Coding | Edit Standard Headers.
 */

using System;
using System.Data;
using System.Data.SQLite;
using System.Collections.Generic;

namespace pontoIndividual
{
    /// <summary>
    /// Description of ponto.
    /// </summary>
    public class ponto
    {
        private DateTime datahora;

        public DateTime Datahora
        {
            get { return datahora; }
            set { datahora = value; }
        }
        private int idponto;

        public int Idponto
        {
            get { return idponto; }
            set { idponto = value; }
        }
        private string observacao;

        public string Observacao
        {
            get { return observacao; }
            set { observacao = value; }
        }

        private tipoPonto tipoponto;
        public tipoPonto Tipoponto
        {
            get { return tipoponto; }
            set { tipoponto = value; }
        }

        public ponto()
        {
        }

        public ponto(int pidPonto, int pidTipoPonto, DateTime pdataHora, string pObservacao)
        {
            tipoPonto tp = new tipoPonto();
            tp.loadTipoFromDb(pidTipoPonto);

            idponto = pidPonto;
            tipoponto = tp;

            datahora = pdataHora;
            observacao = pObservacao;
        }

        public List<registroPonto> listToDay()
        {
            List<registroPonto> t = new List<registroPonto>();

            using (conexao c = new conexao())
            {

                c.open();

                SQLiteCommand cm = new SQLiteCommand("SELECT idponto, (select dscponto from tipoPonto where idtipoponto = P.idtipoponto) as dsctipoponto," +
                                                     " datahora, observacao " +
                                                     " FROM ponto P" +
                                                     " WHERE datahora LIKE @dia ", c.conn);

                cm.Parameters.AddWithValue("@dia", DateTime.Now.ToLocalTime().ToString("dd/MM/yyyy") + "%");

                SQLiteDataReader dr = cm.ExecuteReader();

                while (dr.Read())
                {

                    t.Add(new registroPonto(int.Parse(dr[0].ToString()),
                                            dr[1].ToString(),
                                            DateTime.Parse(dr[2].ToString()),
                                            dr[3].ToString()));

                }

                dr.Close();
                dr.Dispose();
            }

            return t;
        }

        public List<registroPonto> listByFilter(int month, int year)
        {
            List<registroPonto> t = new List<registroPonto>();

            using (conexao c = new conexao())
            {

                c.open();

                SQLiteCommand cm = new SQLiteCommand("SELECT idponto, (select dscponto from tipoPonto where idtipoponto = P.idtipoponto) as dsctipoponto," +
                                                     " datahora, observacao " +
                                                     " FROM ponto P" +
                                                     " WHERE datahora LIKE @dia ",
                                                     c.conn);

                cm.Parameters.AddWithValue("@dia", string.Concat("%/", month.ToString("00"), "/", year.ToString("0000"), "%"));

                SQLiteDataReader dr = cm.ExecuteReader();

                while (dr.Read())
                {

                    t.Add(new registroPonto(int.Parse(dr[0].ToString()),
                                            dr[1].ToString(),
                                            DateTime.Parse(dr[2].ToString()),
                                            dr[3].ToString()));

                }

                dr.Close();
                dr.Dispose();
            }

            return t;
        }



        public bool register()
        {
            int result;

            using (conexao c = new conexao())
            {

                c.open();

                SQLiteCommand cm = new SQLiteCommand("INSERT INTO ponto (idtipoponto, datahora, observacao) " +
                                                     "VALUES (@idtipoponto, @datahora, @observacao)", c.conn);

                cm.Parameters.AddWithValue("@idtipoponto", Tipoponto.IdtipoPonto);
                cm.Parameters.AddWithValue("@datahora", datahora.ToLocalTime().ToString());
                cm.Parameters.AddWithValue("@observacao", observacao);

                result = cm.ExecuteNonQuery();


                cm.Dispose();
                c.close();
            }


            return result > 0;
        }




    }

    public class registroPonto
    {
        private int id;

        public int Id
        {
            get { return id; }
            set { id = value; }
        }
        private string tipo;

        public string Tipo
        {
            get { return tipo; }
            set { tipo = value; }
        }
        private DateTime datahora;

        public DateTime Datahora
        {
            get { return datahora; }
            set { datahora = value; }
        }
        private string observacao;

        public string Observacao
        {
            get { return observacao; }
            set { observacao = value; }
        }

        public registroPonto()
        {

        }

        public registroPonto(int pId, string pTipo, DateTime pDatahora, string pObservacao)
        {

            id = pId;
            tipo = pTipo;
            datahora = pDatahora;
            observacao = pObservacao;
        }


    }
}
